/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.actions;

import java.awt.event.ActionEvent;
import javax.swing.Icon;
import javax.swing.KeyStroke;
import org.jfonia.constants.ActionConstants;
import org.jfonia.images.ImageConstants;
import org.jfonia.images.ScaledImageIcon;
import org.jfonia.language.DescriptionConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.view.main.LeadSheetPlayer;
import org.jfonia.view.main.MainFrame;

/**
 *
 * @author Rik Bauwens
 */
public class PlayOrPauseAction extends PlayerAction
{
        private String playName;
        private String pauseName;
        private Icon playIcon;
        private Icon pauseIcon;

        protected PlayOrPauseAction(int iconMaxSize)
        {
                super(LanguageResource.getInstance().getLabel(LabelConstants.PLAY),
                      new ScaledImageIcon(ImageConstants.PLAY,
                      LanguageResource.getInstance().getDescription(DescriptionConstants.PLAY))
                      .setMaximumSide(iconMaxSize).getImageIcon());
                playName = LanguageResource.getInstance().getLabel(LabelConstants.PLAY);
                pauseName = LanguageResource.getInstance().getLabel(LabelConstants.PAUSE);
                playIcon = new ScaledImageIcon(ImageConstants.PLAY,
                      LanguageResource.getInstance().getDescription(DescriptionConstants.PLAY))
                      .setMaximumSide(iconMaxSize).getImageIcon();
                pauseIcon = new ScaledImageIcon(ImageConstants.PAUSE,
                      LanguageResource.getInstance().getDescription(DescriptionConstants.PAUSE))
                      .setMaximumSide(iconMaxSize).getImageIcon();
                putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(
                        ActionConstants.PLAYORPAUSE_ACTION_KEY,
                        ActionConstants.PLAYORPAUSE_ACTION_MASK));
        }

        public void setPlay()
        {
                putValue(NAME, playName);
                putValue(SMALL_ICON, playIcon);
        }
        
        public void setPause()
        {
                putValue(NAME, pauseName);
                putValue(SMALL_ICON, pauseIcon);
        }

        public void actionPerformed(ActionEvent e)
        {
                if(MainFrame.getInstance().hasLeadSheetFocus() ||
                   MainFrame.getInstance().hasScribblesFocus())
                        return;
                if(LeadSheetPlayer.getInstance().isPlaying())
                {
                        LeadSheetPlayer.getInstance().pause();
                        setPlay();
                }
                else
                {
                        LeadSheetPlayer.getInstance().play();
                        setPause();
                }
        }
}
